package com.singzi.leetcode.N1100;

import java.util.Arrays;
import java.util.LinkedList;
import java.util.Queue;

public class T1034 {
    public static void main(String[] args) {

        int[][] grid = {{1, 2, 1, 2, 1, 2}, {2,2,2,2,1,2},{1,2,2,2,1,2}};
        Solution1034 solution1034 = new Solution1034();
        int []temp = {1,1};
        System.out.println(solution1034.isBorder(grid, temp, 2));
        solution1034.colorBorder(grid, 1,3,1);
        System.out.println(Arrays.deepToString(grid));

    }
}
class Solution1034 {
    public int[][] colorBorder(int[][] grid, int row, int col, int color) {

        int[][] grid2 = new int[grid.length][grid[0].length];

        for (int i = 0; i < grid.length; i++) {
            System.arraycopy(grid[i], 0, grid2[i], 0, grid[0].length);
        }

        Queue<int[]> queue = new LinkedList<>();
        int [][] visited = new int[grid.length+1][grid[0].length+1];
        int []first = new int[2];
        first[0] = row;
        first[1] = col;

        int obj  = grid[row][col];
        queue.add(first);
        while(!queue.isEmpty()){
            int[] temp = queue.remove();

            if(temp[0] == grid.length-1||temp[0] == 0||temp[1] == grid[0].length-1||temp[1]==0) {
                grid[temp[0]][temp[1]] = color;
            }
            else if(isBorder(grid2, temp, obj)){
                grid[temp[0]][temp[1]] = color;
            }
            if(temp[0]<grid.length-1){
                if(grid[temp[0]+1][temp[1]] == obj&&visited[temp[0]+1][temp[1]] == 0){
                    int[] temp1 = new int[2];
                    temp1[0] = temp[0]+1;
                    temp1[1] = temp[1];
                    visited[temp[0]+1][temp[1]] = 1;
                    queue.add(temp1);
                }
            }
            if(temp[0]>0)
            if(grid[temp[0]-1][temp[1]] == obj&&visited[temp[0]-1][temp[1]] == 0){
                int[] temp1 = new int[2];
                temp1[0] = temp[0]-1;
                temp1[1] = temp[1];
                visited[temp[0]-1][temp[1]] = 1;
                queue.add(temp1);
            }
            if(temp[1]<grid[0].length-1)
            if(grid[temp[0]][temp[1]+1] == obj&&visited[temp[0]][temp[1]+1] == 0){
                int[] temp1 = new int[2];
                temp1[0] = temp[0];
                temp1[1] = temp[1]+1;
                visited[temp[0]][temp[1]+1] = 1;
                queue.add(temp1);
            }
            if(temp[1]>0)
            if(grid[temp[0]][temp[1]-1] == obj&&visited[temp[0]][temp[1]-1] == 0){
                int[] temp1 = new int[2];
                temp1[0] = temp[0];
                temp1[1] = temp[1]-1;
                visited[temp[0]][temp[1]-1] = 1;
                queue.add(temp1);
            }
        }

        return grid;
    }
    public Boolean isBorder(int[][] grid, int[] temp, int obj){
        return grid[temp[0] + 1][temp[1]] != obj || grid[temp[0] - 1][temp[1]] != obj || grid[temp[0]][temp[1] + 1] != obj || grid[temp[0]][temp[1] - 1] != obj;
    }
    }


